package com.theory.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.theory.listener.ExcelSubjectListener;
import com.theory.mapper.SubjectMapper;
import com.theory.pojo.Subject;
import com.theory.service.SubjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void importData(String fileName) {
        String path = SubjectServiceImpl.class.getClassLoader().getResource(fileName).getPath();
        EasyExcel.read(path, Subject.class, new ExcelSubjectListener(baseMapper)).sheet().doRead();
    }

    @Override
    public IPage<Subject> listPage(Page<Subject> pageParam, String str) {

        if ("null".equals(str)) {
            return baseMapper.selectPage(pageParam, null);
        }

        QueryWrapper<Subject> subjectQueryWrapper = new QueryWrapper<>();
        subjectQueryWrapper.like("complete", str);

        return baseMapper.selectPage(pageParam, subjectQueryWrapper);
    }
}
